﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shell32;
using System.IO;

namespace BugSysSolution.AnalyzeMediaFile
{
    public partial class Form1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Dictionary<string, string> temp = GetMediaTimeLen(@"D:\bugsys\BugSysSolution\AnalyzeMediaFile\Wildlife.wmv");
            //Dictionary<string, string> temp1 = GetMediaTimeLen(@"D:\bugsys\BugSysSolution\AnalyzeMediaFile\Desert.jpg");

            List<String> list1 = GetSameFolder(@"D:\TestFolder", @"D:\TestFolder 1");
            List<String> list2 = GetDifferentFolder(@"D:\TestFolder", @"D:\TestFolder 1");
        }

        #region VOD project using shell32 get files property

        public Dictionary<string, string> GetMediaTimeLen(string filePath)
        {
            //set Embed Interop Types to false  
            //初始化Shell接口  
            Shell32.Shell shell = new Shell32.ShellClass();
            //获取文件所在父目录对象  
            Folder folder = shell.NameSpace(filePath.Substring(0, filePath.LastIndexOf('\\')));
            //获取文件对应的FolderItem对象  
            FolderItem item = folder.ParseName(filePath.Substring(filePath.LastIndexOf('\\') + 1));

            //字典存放属性名和属性值的键值关系对  
            Dictionary<string, string> Properties = new Dictionary<string, string>();
            int i = 0;
            while (true)
            {
                //获取属性名称
                string key = folder.GetDetailsOf(null, i);
                if (string.IsNullOrEmpty(key))
                {
                    //当无属性可取时，推出循环  
                    break;
                }
                //获取属性值  

                string value = folder.GetDetailsOf(item, i);
                //保存属性
                Properties.Add(key, value);
                i++;
            }
            return Properties;
        }

        protected void btn_getinfo1_Click(object sender, EventArgs e)
        {
            String filePath = @"D:\bugsys\BugSysSolution\AnalyzeMediaFile\test.mp4";
            Folder folder = GetFolder(filePath);
            //获取文件对应的FolderItem对象  
            FolderItem item = GetFolderItem(folder, filePath);

            String temp = folder.GetDetailsOf(item, 28).Replace("kbps", "");
            temp = temp.Substring(1);
            temp = (Convert.ToInt32(temp) * 1000).ToString();

            string time = (((DateTime.Parse("01:01:36") - DateTime.Parse("00:00:00")).TotalMilliseconds) / 1000).ToString();
            long a = long.Parse(time);

            txt_name.Text = folder.GetDetailsOf(item, 0);
            txt_bitrate.Text = temp;// folder.GetDetailsOf(item, 28);
            txt_duration.Text = time.ToString();// folder.GetDetailsOf(item, 27);
            txt_filesize.Text = folder.GetDetailsOf(item, 1);
            txt_frameheight.Text = folder.GetDetailsOf(item, 283);
            txt_framewidth.Text = folder.GetDetailsOf(item, 285);
        }

        protected void btn_getinfo2_Click(object sender, EventArgs e)
        {
            String filePath = @"D:\bugsys\BugSysSolution\AnalyzeMediaFile\Desert.jpg";
            Folder folder = GetFolder(filePath);
            //获取文件对应的FolderItem对象  
            FolderItem item = GetFolderItem(folder, filePath);

            String temp = Convert.ToInt32(folder.GetDetailsOf(item, 164).Replace("pixels", "").Trim().Substring(1)).ToString();
            txt_filename1.Text = folder.GetDetailsOf(item, 0);
            txt_filesize1.Text = folder.GetDetailsOf(item, 1);
            txt_frameheight1.Text = folder.GetDetailsOf(item, 164);
            txt_framewidth1.Text = folder.GetDetailsOf(item, 162);
        }

        public Folder GetFolder(String filePath)
        {
            
            //set Embed Interop Types to false  
            //初始化Shell接口  
            Shell32.Shell shell = new Shell32.ShellClass();
            //获取文件所在父目录对象  
            Folder folder = shell.NameSpace(filePath.Substring(0, filePath.LastIndexOf('\\')));
            return folder;
        }

        public FolderItem GetFolderItem(Folder folder, string filePath)
        {
            return folder.ParseName(filePath.Substring(filePath.LastIndexOf('\\') + 1));
        }

        #endregion

        #region VOD project get the same folder and the different folder from the root folder
        public List<String> GetFolders(String path)
        {
            string[] directories = Directory.GetDirectories(path);
            List<String> list = new List<string>();
            foreach (string name in directories)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(name);
                String fileName = dirInfo.Name;
                if (fileName.Trim().Split('-').Length == 3 && fileName.Trim().Split('-')[0] == "EVDS")
                {
                    list.Add(dirInfo.Name);
                }
            }
            return list;
        }

        public List<String> GetSameFolder(String pathOne, String pathTwo)
        {
            List<String> listOne = GetFolders(pathOne);
            List<String> listTwo = GetFolders(pathTwo);

            List<String> sameItems = new List<string>();

            for (int i = 0; i < listOne.Count; i++)
            {
                for (int j = 0; j < listTwo.Count; j++)
                {
                    if (listOne[i] == listTwo[j])
                    {
                        sameItems.Add(listOne[i]);
                    }
                }
            }
            return sameItems;
        }

        public List<String> GetDifferentFolder(String pathOne, String pathTwo)
        {
            List<String> listOne = GetFolders(pathOne);
            List<String> listTwo = GetFolders(pathTwo);
            List<String> sameItems = GetSameFolder(pathOne, pathTwo);
            List<String> differentItems = new List<string>();
            for (int j = 0; j < sameItems.Count; j++)
            {
                listOne.Remove(sameItems[j]);
                listTwo.Remove(sameItems[j]);
            }

            for (int i = 0; i < listOne.Count; i++)
            {
                differentItems.Add(listOne[i]);
            }
            for (int i = 0; i < listTwo.Count; i++)
            {
                differentItems.Add(listTwo[i]);
            }

            return differentItems;
        }
        #endregion
    }
}